import pandas as pd

from pyecharts import options as opts
from pyecharts.charts import Pie
from pyecharts.charts import Bar
from pyecharts.faker import Faker

def draw_pie(df):
    s1 = list(df1['英雄定位'])
    ding_wei_dict = dict()
    # 统计每个职业出现的次数
    for i in s1:
        if i in ding_wei_dict:
            ding_wei_dict[i] = ding_wei_dict.get(i) + 1
        else:
            ding_wei_dict[i] = 1

    icons = list(ding_wei_dict.keys())
    values = list(ding_wei_dict.values())

    c = (
        Pie()
        .add(
            "",
            [list(z) for z in zip(icons, values)],
            center=["35%", "50%"],
        )
        .set_global_opts(
            title_opts=opts.TitleOpts(title="英雄职业占比图"),
            legend_opts=opts.LegendOpts(pos_left="15%"),
        )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
        .render("htmls/英雄职业占比图.html")
    )

def draw_bar(df):
    hero_list = list(df['英雄名称'])
    skin_list = list(df['皮肤名称'])
    skin_num_list = [len(str(i).split("|")) for i in skin_list]

    res1 = [list(z) for z in zip(hero_list, skin_num_list)]

    def sort_key(s):
        return s[1]

    res2 = sorted(res1,key=sort_key, reverse=True)
    top5 = res2[:5]
    last5 = res2[-5:]
    top5.extend(last5)

    hero_list2 = []
    hero_skin_num = []
    for i in top5:
        hero_list2.append(i[0])
        hero_skin_num.append(i[1])

    # print(Faker.rand_color())
    c = (
        Bar()
        .add_xaxis(hero_list2)
        .add_yaxis("皮肤数量", hero_skin_num, color='#C0D9D9')
        .set_global_opts(
            title_opts=opts.TitleOpts(title="英雄皮肤数量图",subtitle="数量前5和后5"),
            datazoom_opts=[opts.DataZoomOpts(), opts.DataZoomOpts(type_="inside")],
        )
        .render("htmls/英雄皮肤数量图.html")
    )


if __name__ == '__main__':
    # 读取csv数据
    df1 = pd.read_csv('data/王者英雄基本信息.csv')
    # draw_pie(df1)
    draw_bar(df1)



